//
//  DataBase.m
//  JianChaRiBao
//
//  Created by Changxy on 13-6-6.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "DataBase.h"

static NSString * dbName = @"info.db";
static DataBase * shareDatabase = nil;

@implementation DataBase
-(void)dealloc{
    [fmdb close];
    [fmdb release];
    [super dealloc];
}
-(NSString * )pathForName:(NSString *)name{
    NSString * root = NSHomeDirectory();
    root = [root stringByAppendingPathComponent:@"tmp"];
    if (name && [name length] != 0) {
        return [root stringByAppendingPathComponent:name];
    }
    return root;
}

+ (DataBase *)shareDB{
    if (shareDatabase == nil) {
        shareDatabase = [[DataBase alloc]init];
    }
    return shareDatabase;
}

- (id)init{
    self = [super init];
    if (self) {
        NSString * fullPath = [self pathForName:dbName];
        NSLog(@"sql path:%@",fullPath);
        fmdb = [[FMDatabase databaseWithPath:fullPath] retain];
        if ([fmdb open]) {
            NSLog(@"打开成功");
            NSString * tableSql = [NSString stringWithFormat:@"create table if not exists %@(image,itemid,bookname,ncid)",NovelInfo];
            NSString * tableSql1 = [NSString stringWithFormat:@"create table if not exists %@(image,itemid,bookname,ncid)",MagaInfo];
            NSString * tableSql2 = [NSString stringWithFormat:@"create table if not exists %@(image,itemid varchar(255) UNIQUE)",ListenCover];
            if ([fmdb executeUpdate:tableSql] && [fmdb executeUpdate:tableSql1] && [fmdb executeUpdate:tableSql2]) {
                
            }else{
                NSLog(@"建表失败");
            }

        }else{
            NSLog(@"数据库打开失败");
        }
    }
    
    return self;

}

- (BOOL)isExists:(NSDictionary *)dict{
    NSString * existsSql = [NSString stringWithFormat:@"select itemid from NovelInfo where itemid=?"];
    FMResultSet * rs = [fmdb executeQuery:existsSql,[dict objectForKey:@"item_id"]];
    if ([rs next]) {
        return YES;
    }
    return NO;

}
- (void)insertInfo:(NSDictionary *)dict{
    if ([self isExists:dict]) {
        return;
    }
    NSString * insertSql = [NSString stringWithFormat:@"insert into NovelInfo(image,itemid,bookname,ncid) values(?,?,?,?)"];
    NSArray * array = [dict objectForKey:@"chapter_list"];
    NSDictionary * dict1 = [array objectAtIndex:0];
    if ([fmdb executeUpdate:insertSql, [dict objectForKey:@"cover_image"],[dict objectForKey:@"item_id"], [dict objectForKey:@"item_name"],[dict1 objectForKey:@"nc_id"]]) {
        
    }else{
        NSLog(@"插入失败");
    }
}

- (NSArray *)selectInfo{
    NSString * selectSq = [NSString stringWithFormat:@"select * from NovelInfo"];
    NSMutableArray *  array = [NSMutableArray arrayWithCapacity:0];
    FMResultSet * rs = [fmdb executeQuery:selectSq];
    while ([rs next]) {
        NSString * imageStr = [rs stringForColumn:@"image"];
        NSString * itemid = [rs stringForColumn:@"itemid"];
        NSString * bookname = [rs stringForColumn:@"bookname"];
        NSString * ncidStr = [rs stringForColumn:@"ncid"];
        NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:imageStr,@"cover_image",itemid,@"item_id",bookname,@"item_name",ncidStr, @"nc_id", nil];
        [array addObject:dict];
    }
    return array;

}
- (void)deleteAllInfo{
    NSString * deleteSql = [NSString stringWithFormat:@"delete from NovelInfo"];
    if ([fmdb executeUpdate:deleteSql]) {
        
    }else{
        NSLog(@"删除失败");
    }
}
- (BOOL)isExistsMaga:(NSDictionary *)dict{
    NSString * existsSql = [NSString stringWithFormat:@"select itemid from MagaInfo where itemid=?"];
    FMResultSet * rs = [fmdb executeQuery:existsSql,[dict objectForKey:@"item_id"]];
    if ([rs next]) {
        return YES;
    }
    return NO;
    
}

-(void)insertMagaInfo:(NSDictionary *)dict{
    if ([self isExistsMaga:dict]) {
        return;
    }
    NSString * insertSql = [NSString stringWithFormat:@"insert into MagaInfo(image,itemid,bookname,ncid) values(?,?,?,?)"];
    if ([fmdb executeUpdate:insertSql, [dict objectForKey:@"cover_image"],[dict objectForKey:@"item_id"], [dict objectForKey:@"item_name"],[dict objectForKey:@"nc_id"]]) {
        
    }else{
        NSLog(@"插入失败");
    }

}
-(NSArray *)selectMagaInfo{
    NSString * selectSq = [NSString stringWithFormat:@"select * from MagaInfo"];
    NSMutableArray *  array = [NSMutableArray arrayWithCapacity:0];
    FMResultSet * rs = [fmdb executeQuery:selectSq];
    while ([rs next]) {
        NSString * imageStr = [rs stringForColumn:@"image"];
        NSString * itemid = [rs stringForColumn:@"itemid"];
        NSString * bookname = [rs stringForColumn:@"bookname"];
        NSString * ncidStr = [rs stringForColumn:@"ncid"];
        NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:imageStr,@"cover_image",itemid,@"magitemid",bookname,@"item_name",ncidStr, @"nc_id", nil];
        [array addObject:dict];
    }
    return array;
}

- (void)addListen:(NSString *)listenID cover:(NSString *)cover
{
    NSString * insertSql = [NSString stringWithFormat:@"replace into ListenCover(image,itemid) values(?,?)"];
    NSLog(@"addListen>>>>>>%@,%@,%@",insertSql,listenID,cover);
    if ([fmdb executeUpdate:insertSql, cover, listenID]) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}

- (NSString *)getCoverByListen:(NSString *)listenID
{
    NSString * selectSq = [NSString stringWithFormat:@"select * from ListenCover where itemid=%@",listenID];
    NSLog(@"getCoverByListen>>>>>>%@",selectSq);
    FMResultSet *rs = [fmdb executeQuery:selectSq];
    while ([rs next]) {
        NSString *cover = [rs stringForColumn:@"image"];
        NSLog(@"找到封面：%@",cover);
        return cover;
    }
    NSLog(@"没找到封面");
    return nil;
}

@end
